//
//  apple1
//
//  Created by apple on 2021/11/10.
//  Copyright © 2021 zglee. All rights reserved.
//mj综合检测(检测结果)

#import "MJMultipleDetectionResultVC.h"
#import "MJDetectionResultInfoTbvCell.h"
#import "MJCommomResultICell.h"
#import "MemberInfoVC.h"
#import "MJGoutResultICell.h"
#import "MJUMITbvFooterView.h"
#import "PrinterSrarchView.h"

typedef NS_ENUM(NSUInteger, RDetectionItemType) {
    RDetectionItemTypeInfoCell,
    RDetectionItemTypeGoutCell,
    RDetectionItemTypeGluCell,
    RDetectionItemTypeTCCell,
    RDetectionItemTypeHBCell,

};

@interface MJMultipleDetectionResultVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) MJUITableView *tableView;
@property (nonatomic, strong) MJNavBarView *navBarView;
@property (nonatomic, strong) MJMultipleModel *model;
@property (nonatomic, strong) MJUIGradientView *gradientView;

@property (nonatomic, strong) MJTotalCholesterolModel *totalCholesterolModel;
@property (nonatomic, strong) MJGoutDetectionModel *goutDetectionModel;
@property (nonatomic, strong) MJHemoglobinModel *hemoglobinModel;
@property (nonatomic, strong) MJBloodSugarModel *bloodSugarModel;

@property (nonatomic, strong) MJUMITbvFooterView *tbvFootView;

@end

@implementation MJMultipleDetectionResultVC

- (instancetype)initWithModel:(MJMultipleModel *)model{
    if (self = [super init]) {
        self.model = model;
        self.goutDetectionModel = self.model.goutDetection;
        self.bloodSugarModel = self.model.bloodGlucose;
        self.hemoglobinModel = self.model.hemoglobin;
        self.totalCholesterolModel = self.model.totalCholesterol;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self requestListData];
}

- (void)mj_setUI{
    [super mj_setUI];
    
    self.view.backgroundColor = UIColor.mj_backgroundColor;
    [self.view addSubview:self.gradientView];
    [self.view addSubview:self.navBarView];
    [self.view addSubview:self.tableView];
    [self.navBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(STATUS_BAR_HEIGHT);
        make.height.equalTo(@(NAVBAR_HEIGHT));
        make.left.right.equalTo(self.view);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navBarView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }]; 
    [self.navigationController setNavigationBarHidden:true animated:NO];
}

#pragma mark - 方法
- (void)requestListData{
    MJWeakSelf
    NSMutableDictionary *parms = [NSMutableDictionary dictionaryWithDictionary:@{@"current":@(1),@"size":@(1),@"item":@"gout_detection",@"patientId":self.model.patientId}];
    [MJHttpNetWorking requestWithParams:parms requestUri:Detection_List requestType:RequestTypeGet showLoading:false success:^(MJBaseModel *model) {
        
        if (model.code ==  200){
            weakSelf.hideLoading = true;
            NSArray *modelArray =  [NSArray yy_modelArrayWithClass:[MJMultipleModel class] json:model.data];
            if(modelArray.count){
                weakSelf.model = modelArray[0];
                weakSelf.goutDetectionModel = weakSelf.model.goutDetection;
                weakSelf.bloodSugarModel = weakSelf.model.bloodGlucose;
                weakSelf.hemoglobinModel = weakSelf.model.hemoglobin;
                weakSelf.totalCholesterolModel = weakSelf.model.totalCholesterol;
                
                [weakSelf.tableView reloadData];
                weakSelf.tableView.hidden = false;
            }
        }
        } failure:^(NSError *error) {
            
        }];
}

#pragma mark - 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == RDetectionItemTypeGoutCell && !self.goutDetectionModel){
         return 0;
    }
    if(indexPath.row == RDetectionItemTypeGluCell && !self.bloodSugarModel){
         return 0;
    }
    if(indexPath.row == RDetectionItemTypeHBCell && !self.hemoglobinModel){
         return 0;
    }
    if(indexPath.row == RDetectionItemTypeTCCell && !self.totalCholesterolModel){
         return 0;
    }
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MJWeakSelf
    if(indexPath.row == 0){
        MJDetectionResultInfoTbvCell *cell = [MJDetectionResultInfoTbvCell reuseForTableView:tableView forIndexPath:indexPath];
        [cell.infoButton mj_addAction:^(UIButton *button) {
            [HealthHelper jumpMemberInfoViewController:weakSelf patientId:weakSelf.model.patientId];
        }];
        cell.recordDataBaseModel = self.model;
        return cell;
    }
    if(indexPath.row == RDetectionItemTypeGoutCell){
        if(!self.goutDetectionModel){return [UITableViewCell mj_initNullStyle];}
        MJGoutResultICell *cell = [MJGoutResultICell reuseForTableView:tableView forIndexPath:indexPath];
        cell.isFromDetection = self.isFromDetection;
        cell.goutDetectionModel = self.goutDetectionModel;
        cell.itemTitleLabel.mj_localizable = [itemUricAcid stringByAppendingString:@" Testing"];
        cell.rightImvTitleView.hidden = false;
        cell.rightTitleLabel.text = [self.goutDetectionModel.bloodUnit isEqualToString:unitmgdL] ? unitμmolL:unitmgdL;
        [cell.rightImvTitleButton mj_addAction:^(UIButton *button) {
            [cell.goutDetectionModel switchUnit];
            cell.rightTitleLabel.text = [weakSelf.goutDetectionModel.bloodUnit isEqualToString:unitmgdL] ? unitμmolL:unitmgdL;
            [weakSelf.tableView reloadData];
        }];
        return cell;
    }
    if(indexPath.row == RDetectionItemTypeGluCell){
        if(!self.bloodSugarModel){return [UITableViewCell mj_initNullStyle];}
        MJCommomResultICell *cell = [tableView dequeueReusableCellWithIdentifier:@"RDetectionItemTypeGluCell" forIndexPath:indexPath];
        cell.isFromDetection = self.isFromDetection;
        cell.bloodSugarModel = self.bloodSugarModel;
        cell.itemTitleLabel.mj_localizable = [itemBloodGlucose stringByAppendingString:@" Testing"];
        cell.rightImvTitleView.hidden = false;
        cell.rightTitleLabel.text = [self.bloodSugarModel.unit isEqualToString:unitmgdL] ? unitmmolL:unitmgdL;
        [cell.rightImvTitleButton mj_addAction:^(UIButton *button) {
            [cell.bloodSugarModel switchUnit];
            cell.rightTitleLabel.text = [weakSelf.bloodSugarModel.unit isEqualToString:unitmgdL] ? unitmmolL:unitmgdL;
            [weakSelf.tableView reloadData];
        }];
        return cell;
    }
    if(indexPath.row == RDetectionItemTypeHBCell){
        if(!self.hemoglobinModel){return [UITableViewCell mj_initNullStyle];}
        MJCommomResultICell *cell = [tableView dequeueReusableCellWithIdentifier:@"RDetectionItemTypeHBCell" forIndexPath:indexPath];
        cell.isFromDetection = self.isFromDetection;
        cell.hemoglobinModel = self.hemoglobinModel;
        cell.itemTitleLabel.mj_localizable = [itemHemoglobin stringByAppendingString:@" Testing"];
//        cell.rightImvTitleView.hidden = false;
        cell.rightTitleLabel.text = [self.hemoglobinModel.unit isEqualToString:unitgdL] ? unitmmolL:unitgdL;
        [cell.rightImvTitleButton mj_addAction:^(UIButton *button) {
            [cell.hemoglobinModel switchUnit];
            cell.rightTitleLabel.text = [weakSelf.hemoglobinModel.unit isEqualToString:unitgdL] ? unitmmolL:unitgdL;
            [weakSelf.tableView reloadData];
        }];
        return cell;
    }
    if(indexPath.row == RDetectionItemTypeTCCell){
        if(!self.totalCholesterolModel){return [UITableViewCell mj_initNullStyle];}
        MJCommomResultICell *cell = [tableView dequeueReusableCellWithIdentifier:@"RDetectionItemTypeTCCell" forIndexPath:indexPath];
        cell.isFromDetection = self.isFromDetection;
        cell.totalCholesterolModel = self.totalCholesterolModel;
        cell.itemTitleLabel.mj_localizable = [itemBloodLipid stringByAppendingString:@" Testing"];
        cell.rightImvTitleView.hidden = false;
        cell.rightTitleLabel.text = [self.totalCholesterolModel.unit isEqualToString:unitmgdL] ? unitmmolL:unitmgdL;
        [cell.rightImvTitleButton mj_addAction:^(UIButton *button) {
            [cell.totalCholesterolModel switchUnit];
            cell.rightTitleLabel.text = [weakSelf.totalCholesterolModel.unit isEqualToString:unitmgdL] ? unitmmolL:unitmgdL;
            [weakSelf.tableView reloadData];
        }];
        return cell;
    }
    return [UITableViewCell mj_initNullStyle];
}

#pragma mark - 懒加载
- (MJUITableView *)tableView{
    if (!_tableView) {
        MJUITableView *tableView = [[MJUITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorInset= UIEdgeInsetsMake(0,0, 0, 0);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.backgroundColor = UIColor.clearColor;
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.estimatedRowHeight = 140;
        
        [MJDetectionResultInfoTbvCell registerNibWithTableView:tableView];
        [MJGoutResultICell registerNibWithTableView:tableView];
        
        //cell不复用
        [tableView registerNib:[UINib nibWithNibName:@"MJCommomResultICell" bundle:nil] forCellReuseIdentifier:@"RDetectionItemTypeTCCell"];
        [tableView registerNib:[UINib nibWithNibName:@"MJCommomResultICell" bundle:nil] forCellReuseIdentifier:@"RDetectionItemTypeHBCell"];
        [tableView registerNib:[UINib nibWithNibName:@"MJCommomResultICell" bundle:nil] forCellReuseIdentifier:@"RDetectionItemTypeGluCell"];
        tableView.tableFooterView = self.tbvFootView;

        _tableView = tableView;
    }
    return _tableView;
}
- (MJUMITbvFooterView *)tbvFootView {
    if (!_tbvFootView) {
        _tbvFootView = [MJUMITbvFooterView mj_loadNibWithOwner:self];
        _tbvFootView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100);
        _tbvFootView.autoresizingMask = UIViewAutoresizingNone;
    }
    return _tbvFootView;
}
- (MJNavBarView *)navBarView {
    if (!_navBarView) {
        MJWeakSelf
        _navBarView = [MJNavBarView mj_loadNibWithOwner:self];
        [_navBarView.btnBack mj_addAction:^(UIButton *btn) {
            [weakSelf mj_popViewController];
        }];
        _navBarView.rightImvTitleView.hidden = false;
        _navBarView.rightTitleLabel.hidden = true;
        _navBarView.rightTitleImageView.image = MJUIImageMake(@"icon_print");
        [_navBarView.rightImvTitleButton mj_addAction:^(UIButton *button) {
            //
            PrinterSrarchView *printerSrarchView = [[PrinterSrarchView alloc] init];
            printerSrarchView.multipleModel = weakSelf.model;
            printerSrarchView.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            weakSelf.modalPresentationStyle = UIModalPresentationCurrentContext;
            [weakSelf.navigationController presentViewController:printerSrarchView animated:NO completion:nil];
        }];
        
        _navBarView.labTitle.mj_localizable = [HealthHelper detectionTitle:DetectionItemTypeMultiple];
    }
    return _navBarView;
}

- (MJUIGradientView *)gradientView {
    if (!_gradientView) {
        MJUIGradientView *view = [[MJUIGradientView alloc] init];
        view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 200);
        _gradientView = view;
    }
    return _gradientView;
}

@end
 
